=begin

This file is part of the arena-ruby-sandbox project, http://arena-ruby-sandbox.googlecode.com

Copyright (c) 2009-2009 Roberto Dario Seminara <robertodarioseminara@gmail.com>

arena-ruby-sandbox is free software: you can redistribute it and/or modify
it under the terms of the gnu general public license as published by
the free software foundation, either version 3 of the license, or
(at your option) any later version.

arena-ruby-sandbox is distributed in the hope that it will be useful,
but without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.  see the
gnu general public license for more details.

you should have received a copy of the gnu general public license
along with arena-ruby-sandbox.  if not, see <http://www.gnu.org/licenses/>.
=end

module RubySandbox
module IntrospectionDisable
	def instance_eval
		raise "Illegal use of instance_eval"
	end
	
	def self.deactivate_instance_eval
		
		secret_method_name = "_" + rand(10000000000000000000000000000000).to_s
		
		eval("
		class ::Object
			alias :#{secret_method_name} :instance_eval
			undef :instance_eval
		end
		")
		
		secret_method_name
	end
	
	def self.activate_instance_eval( secret_method_name) 
		eval("
		class ::Object
			alias :instance_eval :#{secret_method_name} 
			undef :#{secret_method_name}
		end
		")
	end


	def self.deactivate_singleton_method_added
		secret_method_name = "_" + rand(10000000000000000000000000000000).to_s

		eval("
		class ::Object
			alias :#{secret_method_name} :singleton_method_added
			def singleton_method_added(m)
				disable_privileges
			end
		end
		")

		secret_method_name
	end

	def self.activate_singleton_method_added( secret_method_name) 
		eval("
		class ::Object
			undef :singleton_method_added
			alias :singleton_method_added :#{secret_method_name} 
			undef :#{secret_method_name}
		end
		")
	end

end

module DisablePrivileges
	def disable_privileges

		permitted_methods = ["initialize","Array", "Float", "Integer", "String", "__method__",
			"sleep", "local_variables", "proc", "lambda", "loop", "throw", "raise", "rand",
			 "format", "print", "printf", "srand", "singleton_method_added","eval", "block_given?"]
		# anular todos los metodos privados excepto los estrictamente necesarios del objeto

		(self.private_methods - permitted_methods).each do |m|
			eval("def self.#{m}(*x); raise NoMethodError.new; end")
		end

	end
end

class MethodlessObject

	permitted_methods = ["initialize","raise", "eval", "block_given?", "singleton_method_added", "inspect"]

	(MethodlessObject.private_instance_methods - permitted_methods).each do |m|
		define_method(m) do
			raise NoMethodError.new
		end
	end
	
	(MethodlessObject.public_instance_methods - ["inspect", "to_s", "private_methods"]).each do |m|
		define_method(m) do
			raise NoMethodError.new
		end
	end
end

end

class Object

	include RubySandbox::DisablePrivileges

	class << self
		def set_constant(key,value)
			eval("::#{key} = value")
		end
		def delete_constant(c)
			remove_const(c.to_sym)
		end
	end
end

